package com.cr.admin.system.controller;

import com.cr.admin.system.pojo.dto.SysDictOptionAddParam;
import com.cr.admin.system.pojo.dto.SysDictOptionQuery;
import com.cr.admin.system.pojo.dto.SysDictOptionUpdateParam;
import com.cr.admin.system.pojo.entity.SysDictOption;
import com.cr.admin.system.service.ISysDictOptionService;
import com.cr.common.response.JsonResult;
import com.cr.common.response.PageData;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "65.字典项管理")
@RestController
@RequestMapping("/api/sys/dict/options")
public class SysDictOptionController {

    @Autowired
    private ISysDictOptionService sysDictOptionService;

    @ApiOperation(value = "新增字典项")
    @ApiOperationSupport(order = 100)
    @PostMapping
    public JsonResult add(@Validated @RequestBody SysDictOptionAddParam sysDictOptionAddParam) {
        sysDictOptionService.insert(sysDictOptionAddParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "删除字典项")
    @ApiOperationSupport(order = 200)
    @DeleteMapping
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        sysDictOptionService.deleteByIds(ids);
        return JsonResult.success();
    }

    @ApiOperation(value = "修改字典项")
    @ApiOperationSupport(order = 300)
    @PutMapping
    public JsonResult update(@Validated @RequestBody SysDictOptionUpdateParam sysDictOptionUpdateParam) {
        sysDictOptionService.updateById(sysDictOptionUpdateParam);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询字典项详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        SysDictOption sysDictOption = sysDictOptionService.selectById(id);
        return JsonResult.success(sysDictOption);
    }

    @ApiOperation(value = "查询字典项列表")
    @ApiOperationSupport(order = 410)
    @GetMapping
    public JsonResult list(Integer pageNum, Integer pageSize, SysDictOptionQuery query) {
        PageData<SysDictOption> pageData = PageData.page(pageNum, pageSize, () -> sysDictOptionService.selectList(query));
        return JsonResult.success(pageData);
    }

}
